<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        #div {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="div"></div>

    <script>
        div.onmousedown = function(event) {
            event = event || window.event;
            let x = event.clientX - div.offsetLeft;
            let y = event.clientY - div.offsetTop;
            document.onmousemove = function(event) {
                div.style.left = (event.clientX - x) + "px";
                div.style.top = (event.clientY - y) + "px"
            }
            div.onmouseup = function() {
                document.onmousemove = null;
            }
        }
    </script>
</body>

</html>